package com.yurui.service.impl;

import com.yurui.entity.Order;
import com.yurui.mapper.OrderMapper;
import com.yurui.service.AccountService;
import com.yurui.service.OrderService;
import com.yurui.service.StorageService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : yurui
 * @date : 2023/10/22
 * @TIME : 16:42
 * @project : parentCloud
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;


    @Override
    @GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        System.out.println("全局事务ID=" + RootContext.getXID());
        log.info("--------开始新建订单----------");
        orderMapper.create(order);

        log.info("--------开始调用库存微服务，做货物扣减----------");
        storageService.decrease(order.getProductId(), order.getCount());

        log.info("--------开始调用账户微服务，做金额扣减----------");
        accountService.decrease(order.getUserId(), order.getMoney() * order.getCount());

        log.info("--------修改订单状态----------");
        orderMapper.update(order.getUserId(), order.getStatus());
        //在此处加断点可以查看undo log中的信息
        log.info("--------订单完成----------");
    }
}
